{% extends 'myadmin/base.html' %}
{%block body%}
<div class="tpl-content-wrapper">
            <div class="tpl-content-page-title">
                Amaze UI 文字列表
            </div>
            <ol class="am-breadcrumb">
                <li><a href="#" class="am-icon-home">首页</a></li>
                <li><a href="#">Amaze UI CSS</a></li>
                <li class="am-active">文字列表</li>
            </ol>
            <div class="tpl-portlet-components">
                <div class="portlet-title">
                    <div class="caption font-green bold">
                        <span class="am-icon-code"></span> 列表
                    </div>
                    <div class="tpl-portlet-input tpl-fz-ml">
                        <div class="portlet-input input-small input-inline">
                            
                        </div>
                    </div>


                </div>
                <div class="tpl-block">
                    <form action="">
                    <div class="am-g">
                        <div class="am-u-sm-12 am-u-md-6">
                            <div class="am-btn-toolbar">
                                
                            </div>
                        </div>
                        <div class="am-u-sm-12 am-u-md-3">
                            <div class="am-form-group">
                                <select name="type" data-am-selected="{btnSize: 'sm'}" style="display: none;">
                                    <option {% if request.GET.type == 'all' %}selected {% endif %} value="all">所有类别</option>
                                    <option {% if request.GET.type == 'cname' %}selected {% endif %} value="name">商品类别</option>
                                </select>
                            </div>
                        </div>
                        <div class="am-u-sm-12 am-u-md-3">
                            <div class="am-input-group am-input-group-sm">
                                <input type="text" value="{{request.GET.search}}" name="search" class="am-form-field ">
                                <span class="am-input-group-btn">
                                    <button class="am-btn  am-btn-default am-btn-success tpl-am-btn-success am-icon-search">
                                    </button>
                                </span>
                            </div>
                        </div>
                    </div>
                    </form>
                    <div class="am-g">
                        <div class="am-u-sm-12">
                            <form class="am-form">
                                <table class="am-table am-table-striped am-table-hover table-main">
                                    <thead>
                                        <tr>
                                            <th>id</th>
                                            <th>cname</th>
                                            <th>cid</th>
                                            <th>cpath</th>
                                            <th>操作</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                    {% for i in cates %}
                                        <tr class="gradeX">
                                            <td>{{i.id}}</td>
                                            <td class="dbl" id="{{i.id}}">{{i.newname}}<span>{{i.cname}}</span></td>
                                            <td>{{i.cid}}</td>
                                            <td>{{i.cpath}}</td>

                                           
                                            <td>
                                                <div class="tpl-table-black-operation">
                                                    <a href="" >
                                                        <i class="am-icon-pencil"></i> 编辑
                                                    </a>
                                                    <a pid="{{i.id}}"  class="delcate tpl-table-black-operation-del">
                                                        <i class="am-icon-trash"></i> 删除
                                                    </a>
                                                </div>
                                            </td>
                                        </tr>
                                    {% endfor %}
                                    <!-- more data -->
                                    </tbody>
                                   
                                </table>

                                <div class="am-cf">

                                    <div class="am-fr">
                                        <ul class="am-pagination tpl-pagination">
                                            <li ><a href="{% url 'myadmin_categorylist' %}?p=1&type={{request.GET.type}}&search={{request.GET.search}}">首页</a></li>

                                            <li {% if page == 1 %}class="am-disabled"{% endif %}><a href="{% url 'myadmin_categorylist' %}?p={{page|add:-1}}&type={{request.GET.type}}&search={{request.GET.search}}">«</a></li>
                                            {% for i in prange %}
                                            <li {% if page == i %} class="am-active" {% endif %}><a href="{% url 'myadmin_categorylist' %}?p={{i}}&type={{request.GET.type}}&search={{request.GET.search}}">{{i}}</a></li>
                                            {% endfor %}
                                            <li {% if page == sumpage %}class="am-disabled"{% endif %}><a href="{% url 'myadmin_categorylist' %}?p={{page|add:1}}&type={{request.GET.type}}&search={{request.GET.search}}">»</a></li>

                                            <li ><a href="{% url 'myadmin_categorylist' %}?p={{sumpage}}&type={{request.GET.type}}&search={{request.GET.search}}">尾页</a></li>
                                        </ul>
                                    </div>
                                </div>
                                <hr>

                            </form>
                        </div>

                    </div>
                </div>
                <div class="tpl-alert"></div>
            </div>

        </div>




{%endblock%}
{% block script%}
         <script type="text/javascript">
                                       // 给删除添加单击事件
                                       $('.delcate').click(function(){
                                            // 获取pid
                                            var pid = $(this).attr('pid')
                                            var el = $(this).parents('tr')
                                            // 向后台发送删除的请求
                                            $.get('{% url 'myadmin_delcate' %}',{pid:pid},function(data){

                                                // 如果成功就删除当前这一行的标签
                                                if(data['msg']==0){
                                                    alert('当前分类下有子类，不能删除')
                                                }else if(data['msg']==1){
                                                    // 删除元素
                                                    el.remove()
                                                }
                                            },'json')

                                       })

                                       // 修改
                                       $('.dbl').dblclick(function(){
                                            // 获取当前单机的标签
                                            var sp = $(this).find('span')
                                            // 获取id
                                            var id = $(this).attr('id')
                                            // 获取现有的name
                                            var val = $(this).find('span').text()
                                            // 创建input表单
                                            var inp = $('<input type="text" class="inp" value='+val+'>')
                                            inp.css('color','#000')
                                            // 将创建好的input写入到页面
                                            $(this).find('span').html(inp)

                                            // 失去焦点是假
                                            inp.blur(function(){
                                                // 获取当前input的value值
                                                var newval = $(this).val()
                                                // 判断有没有修改
                                                if(newval==val){
                                                    // 将原来的值写入span
                                                    $(this).parent().html(val)
                                                }else{
                                                     // 发送请求
                                                    $.get('{% url 'myadmin_editcate' %}',{id:id,name:newval},function(data){
                                                        // 判断有没有添加成功
                                                        if(data['msg']==1){
                                                            console.log('ok')
                                                            sp.html(newval)
                                                        }else{
                                                            sp.html(val)
                                                            alert('修改失败')
                                                        }
                                                    })
                                                }                                    
                                                
                                            })

                                       })

                                    </script>
{% endblock %}